package com.wandian.cqcsk.mvvm.mine.invite.invitereward;

import android.os.Bundle;
import android.text.Html;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.StatusBarUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.databinding.ActivityInviteRewardBinding;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 邀请有奖
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_INVITE_REWARD)
public class InviteRewardActivity extends BaseMVVMActivity<InviteRewardViewModel> {
    private ActivityInviteRewardBinding dataBinding;
    private UserInfoBean userInfoBean;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_invite_reward);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        userInfoBean = AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
        initViewData();
        initClink();
        mViewModel.myInvitation();
    }
    private void initViewData(){
        //设置状态颜色和字体黑色
        StatusBarUtils.with(this,false)
                .setIsActionBar(false)
                .clearActionBarShadow()
                .setDrawable(getResources().getDrawable(com.mylib.libcore.R.drawable.sys_color))
                .init();
        dataBinding.bindSysToolBar.toolBarTvTitle.setText("邀请有奖");
        dataBinding.bindSysToolBar.tvRight.setText("邀请好友");
        dataBinding.bindSysToolBar.tvRight.setVisibility(View.VISIBLE);
        dataBinding.tvInviteZy.setText(Html.fromHtml("1.邀请<strong><font color=\"#ff0000\">1</font></strong>名好友，使用微信登录注册并参与打卡，便可获得<strong><font color=\"#ff0000\">0.5</font></strong>元奖励"));
      try {
          dataBinding.tvMoney.setText(((double)Integer.parseInt(userInfoBean.getGoldNum())/100)+"");
      }catch (Exception e){}

      mViewModel.listData.observe(this,bean->{
          if(!UIUtils.isEmpty(bean) &&bean.size()>0)
              dataBinding.tvInviteNum.setText(Html.fromHtml("好友<strong><font color=\"#FA5444\">"+bean.size()+"</font></strong>人"));
      });
    }
    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        //邀请记录
        dataBinding.tvInviteHistory.setOnClickListener(v -> {
            if(!UIUtils.isEmpty(mViewModel.listData.getValue()) &&  mViewModel.listData.getValue().size()>0){
                if(mViewModel.listData.getValue().size()>=5){
                    dataBinding.tvTaskState1.setText("已完成");
                    dataBinding.progress1.setProgress(5);
                    dataBinding.tvTaskState1.setTextColor(getResources().getColor(R.color.text_blue));
                }
                else dataBinding.progress1.setProgress(mViewModel.listData.getValue().size());
                ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_INVITE_RECORD).withString("friends_json", JsonUtil.toJson(mViewModel.listData.getValue())).navigation();
            }else {
                ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_INVITE_RECORD).withString("friends_json", "").navigation();
            }
        });
        //邀请规则
        dataBinding.tvInviteRule.setOnClickListener(v -> ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_INVITE_RULE).navigation());
        //邀请
        dataBinding.bindSysToolBar.tvRight.setOnClickListener(v ->  ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_INVITE).navigation());
    }


}
